package com.qigao.mall.channel.weichat;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qigao.mall.channel.ChannelCore;
import com.qigao.mall.channel.config.ChannelConstant;
import com.qigao.mall.channel.dto.ChannelRefundRespDto;
import com.qigao.mall.commons.dto.MerChannelDto;
import com.qigao.mall.commons.enums.ChannelActionEnum;
import com.qigao.mall.commons.enums.ChannelCodeEnum;
import com.qigao.mall.commons.enums.ChannelInfoEnum;
import com.qigao.mall.commons.enums.PayProductEnum;
import com.qigao.mall.commons.param.ChannelReqParam;
import com.qigao.mall.commons.param.ChannelRespParam;
import com.qigao.mall.commons.utils.HttpClientUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 文档链接：https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_20&index=1
 */
@Service
public class WeichatRefund extends AbstractWeichatTrade implements ChannelCore {

    @Resource
    ChannelConstant channelConstant;
    private static final Logger logger= LoggerFactory.getLogger(WeichatRefund.class);

    @Override
    public boolean isSupport(PayProductEnum payProductEnum, ChannelActionEnum channelActionEnum, ChannelInfoEnum channelInfoEnum) {
        return ChannelActionEnum.REFUND.equals(channelActionEnum) && channelInfoEnum.equals(ChannelInfoEnum.WEICHAT);
    }

    @Override
    protected String getSelfReqUrl() {
        return channelConstant.getWeichatRefundUrl();
    }

    public ChannelRespParam channelProcess(MerChannelDto merChannelDto, ChannelReqParam channelReqParam) {
        //1.构造请求参数
        String reqContent=buildReqParam(merChannelDto,channelReqParam);
        //2.发送请求
        String reqUrl=getSelfReqUrl()==null?channelConstant.getWeichatOrderUrl():getSelfReqUrl();
        String respContent= HttpClientUtil.postXmlWithKey(reqUrl,reqContent,merChannelDto.getResvKey(),merChannelDto.getChannelPartenerNo());
        logger.info("[微信交易]返回内容:{}",respContent);
        //3.处理响应
        ChannelRespParam channelRespParam=this.convertRespData(respContent,merChannelDto);
        logger.info("[微信交易]返回转换后内容:{}",channelRespParam);
        //3.处理响应
        return channelRespParam;
    }

    @Override
    public Map buildSelfReqParam(MerChannelDto merChannelDto, ChannelReqParam channelReqParam) {
        Map reqParam=new ConcurrentHashMap<String,String>();
        JSONObject reqContent= JSONObject.parseObject(channelReqParam.getReqContent());
        reqParam.put("out_trade_no",merChannelDto.getInnerSeqNo());
        reqParam.put("out_refund_no",merChannelDto.getInnerRefundSeqNo());//
        reqParam.put("total_fee", reqContent.getString("payAmtFen"));//订单总金额，单位为分，详见支付金额
        reqParam.put("refund_fee",reqContent.getString("refundAmtFen"));//退款总金额，订单总金额，单位为分，只能为整数
        reqParam.put("notify_url",channelConstant.getWeichatRefundNotifyUrl());
        return reqParam;
    }

    @Override
    protected ChannelRespParam buildSelfRespData(Map respMap, MerChannelDto merChannelDto) {
        ChannelRefundRespDto channelRefundRespDto =new ChannelRefundRespDto();
        channelRefundRespDto.setThirdTradeNo((String) respMap.get("transaction_id"));
        channelRefundRespDto.setInnerSeqNo((String)respMap.get("out_trade_no"));
        BigDecimal refundAmt=new BigDecimal((String)respMap.get("refund_fee"));
        channelRefundRespDto.setRefundAmt(refundAmt);
        channelRefundRespDto.setThirdRefundNo((String) respMap.get("refund_id"));
        channelRefundRespDto.setInnerRefundNo((String)respMap.get("out_refund_no"));
        String retStr = JSON.toJSONString(channelRefundRespDto);
        ChannelRespParam channelRespParam=new ChannelRespParam();
        channelRespParam.setRespCode(ChannelCodeEnum.SUCCESS.getCode());
        channelRespParam.setRespMsg(ChannelCodeEnum.SUCCESS.getName());
        channelRespParam.setRespContent(retStr);
        return  channelRespParam;
    }

}
